<template>
  <div class="app-container">
    <!-- 查询行 -->
    <el-row :gutter="10" class="mb8 query-row">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="目录" prop="dir">
          <el-select v-model="queryParams.dir" placeholder="请选择目录" clearable @change="handleQuery" size="mini" style="width: 240px">
            <el-option v-for="dir in dirOptions" :key="dir" :label="dir" :value="dir" />
          </el-select>
        </el-form-item>
        <el-form-item label="参数名" prop="parmname">
          <el-input v-model="queryParams.parmname" placeholder="请输入参数名称" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="中文名称" prop="zwName">
          <el-input v-model="queryParams.zwName" placeholder="请输入中文名称" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="运行中" prop="isRun">
          <el-checkbox v-model="queryParams.isRun" size="mini" @change="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 按钮行 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-tooltip content="重新加载所有JSON配置文件,含flow,sql,config等" placement="top-start" effect="dark">
          <el-button type="primary" plain icon="el-icon-refresh-right" size="mini" :loading="loading"
            @click="handleReload">
            加载配置
          </el-button>
        </el-tooltip>
      </el-col>

      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-video-play" size="mini" :loading="loading"
          @click="handleRunFlow" :disabled="!currentRow">
          运行流程({{ currentRow && currentRow.parmname }})
        </el-button>
      </el-col>
      
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-document" size="mini" :loading="loading" 
          @click="handleFlowInfo" :disabled="!currentRow">
          流程配置
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-document" size="mini" :loading="loading" 
          @click="handleRunTimeInfo" :disabled="!currentRow">
          运行信息
        </el-button>
      </el-col>
      
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="" size="mini" :loading="loading" @click="handleAITLog">
          <svg-icon icon-class="log" /> 接口方法日志({{ currentRow && currentRow.parmname }})
        </el-button>
      </el-col> -->

      <el-col :span="1.5">
        <el-tooltip content="查看服务器各类日志" placement="top-start" effect="dark">
          <el-button type="info" plain icon="" size="mini" :loading="loading" @click="handleLogFile">
            <svg-icon icon-class="log" /> 服务器日志
          </el-button>
        </el-tooltip>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="mainTable" v-loading="loading" :data="dataList" @row-dblclick="handleDetail" :row-style="{ height: '32px' }"
      :cell-style="{ padding: '0px' }" border :height="tableHeight" highlight-current-row
      @current-change="handleCurrentChange" :row-class-name="tableRowClassName">
      <el-table-column label="目录" width="80" align="center" prop="dir" fixed/>
      <el-table-column label="参数名" width="220" align="left" prop="parmname" fixed show-overflow-tooltip sortable/>
      <el-table-column label="中文名称" width="180" align="center" prop="zwName" fixed show-overflow-tooltip/>
      <el-table-column label="文件名" width="160" align="center" prop="filename" show-overflow-tooltip sortable/>
      <el-table-column label="类型" width="60" align="center" prop="type"/>
      <el-table-column label="运行状态" width="100" align="center" prop="statusName" sortable/>
      <el-table-column label="流程ID" width="200" align="center" prop="flowID" sortable/>
      <el-table-column label="运行(毫秒)" width="120" align="center" prop="runTime" sortable/>
      <el-table-column label="开始时间" width="180" align="center" prop="startTime" sortable/>
      <el-table-column label="结束时间" width="180" align="center" prop="endTime" sortable/>
      <el-table-column label="描述" width="280" align="left" prop="desc" />
      <el-table-column label="路径" width="360" align="left" prop="path" show-overflow-tooltip />
    </el-table>

    <!-- 查看流程配置,通过属性传入参数 -->
    <flow-info-dlg :visible.sync="flowOpen" :code="currentRow && currentRow.parmname"></flow-info-dlg>
    <!-- 服务器日志,通过方法传入参数 -->
    <ServerLogDlg ref="ServerLogDlg"></ServerLogDlg>
    <!-- 运行信息 -->
    <RunTimeDlg ref="RunTimeDlg"></RunTimeDlg>

  </div>
</template>

<script>
import { list, dir, reload, run } from "@/api/aitcommon/aitconfig";
import { objectEqual } from "@/utils/ait";
import FlowInfoDlg from '@/components/AIT/FlowInfo';
import ServerLogDlg from '@/components/AIT/ServerLog';
import RunTimeDlg from '@/components/AIT/RunTimeDlg';

export default {
  name: "aitconfig",
  components: { FlowInfoDlg,ServerLogDlg,RunTimeDlg },
  data() {
    return {
      loading:false,
      // 显示搜索条件
      showSearch: true,
      //表格高度
      tableHeight: 500,
      // 查询参数
      queryParams: {},
      // 数据
      dataList: [],
      // 目录
      dirOptions: [],
      // 当前行
      currentRow: null,
      // 流程详情
      flowOpen: false,
    }
  },
  created() {
    //获取目录
    this.getDir();
    //获取数据
    if(!this.$route.params.queryParams){
      this.getList();
    }
  },
  activated(){
    // https://blog.csdn.net/cnailili/article/details/119038528
    if(this.$route.params.queryParams){
      let {queryParams}=this.$route.params;
      // 比较参数是否相同,相同则不刷新
      if(!objectEqual(this.queryParams,queryParams)){
        this.queryParams=queryParams;
        this.getList();
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      // window窗口大小变化后,页面table自适应高度,需要点击 刷新
      // window.innerHeight 浏览器窗口的可见高度，下面的 220 是除了table最大高度的剩余空间。
      let height = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 220;
      this.tableHeight = height;
    })
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      // tablerow的样式,需要在ait.scss中设置
      let className="";
      if (row.status == 0) {
        // 关闭
        if(row.logStatus==0)//INFO
          return className+=' complete-row ';
        else if(row.logStatus==1)//WARN
          return className+=' warn-row ';
        else if(row.logStatus==2)//ERROR
          return className+=' abnormal-row ';
      } else if (row.status == 1) {
        // 运行中
        if (row.runTime < this.timeSec * 1000) {
          return className+=' normal-row ';
        } else {
          // 超长运行,警告
          return className+=' warn-row ';
        }
      }
      return className;
    },
    handleLogFile() {
      // 通过调用子控件方法,弹出窗口,并传递参数
      this.$refs.ServerLogDlg.openDlg('common');
    },
    handleAITLog() {
      // console.info('handleAITLog',this.currentRow)
      if (!this.currentRow) {
        this.msgError('请先选择一条记录!')
        return;
      }
      if (!this.currentRow.flowID) {
        this.msgError('流程ID为空,请先运行流程')
        return;
      }
      // 跳转指定页面,带入查询参数
      this.$router.push({
        path: "/ait/aitlog",
        query: {
          flowId: this.currentRow.flowID
        }
      });
    },
    handleRunFlow() {
      if (this.currentRow && this.currentRow.parmname) {
        this.loading = true;
        run(this.currentRow.parmname).then(res => {
          // this.getList();
          // 显示执行结果
          // 改成异步调用,弹出窗体时可能尚未执行或还在执行
          this.$refs.RunTimeDlg.openDlg(this.currentRow.parmname,this.getList);
        }).finally(res => {
          this.loading = false;
        })
      }
    },
    handleReload() {
      this.loading = true;
      reload().then((res) => {
        this.getList();
        this.msgSuccess('配置文件已经重新加载!')
      }).finally(res=>{
        this.loading = false;
      });
    },
    handleFlowInfo() {
      if (!this.currentRow) {
        this.$message({
          message: '请先选择一条记录!',
          type: 'warning'
        });
        return;
      }
      this.flowOpen = true;
    },
    handleRunTimeInfo() {
      if (!this.currentRow) {
        this.$message({
          message: '请先选择一条记录!',
          type: 'warning'
        });
        return;
      }
      // console.info('currentRow',this.currentRow)
      // 通过调用子控件方法,弹出窗口,并传递参数
      this.$refs.RunTimeDlg.openDlg(this.currentRow.parmname,this.getList);
    },
    handleCurrentChange(val) {
      // val为空时,保留原焦点行
      this.currentRow = val||this.currentRow;
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.handleFlowInfo();
    },
    // 获取目录选项
    getDir() {
      // console.info('getdir',dir)
      this.loading = true;
      dir().then((response) => {
        this.dirOptions = response.data;
        this.loading = false;
      });
    },
    /** 查询接口日志列表 */
    getList() {
      console.info('getlist',this.queryParams);
      this.loading = true;
      list(this.queryParams).then((response) => {
        //重置数据
        this.dataList = response.data;
        // console.info('getlist',response.data,this.dataList,this.currentRow)
        // 重置焦点行,依据 参数名
        if(this.dataList&&this.currentRow){
          this.currentRow=this.dataList.find(item=>item.parmname===this.currentRow.parmname)
          if(this.currentRow){
            this.$refs.mainTable.setCurrentRow(this.currentRow)
          }
        }
      }).finally(res=>{
        this.loading = false;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {};
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.queryParams.pageNum = 1;
      this.getList();
    },
  }
}
</script>

